package com.theaf.basemodule.web.controller;

import com.theaf.basemodule.common.api.AreaService;
import com.theaf.basemodule.common.condition.AreaCondition;
import com.theaf.basemodule.common.dto.AreaDTO;
import com.theaf.basemodule.common.vo.AreaVO;
import com.theaf.core.web.ResultHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:songwenke
 * @Description:地区Controller
 * @Date:create in 16:15 2017/12/25
 * @Modified By:
 **/
@RestController
@RequestMapping(value="/area")
@Api(description="地区controller")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @RequestMapping(value = "/list",  method = RequestMethod.POST)
    @ApiOperation( value="地区列表查询", notes = "地区列表查询", response = AreaDTO.class)
    public Object list(@RequestBody @ApiParam(name="查询条件",value="传入json格式",required=true) AreaCondition condition){
        return ResultHandler.success(areaService.findArea(condition));
    }

    @RequestMapping(value = "/child/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "下级地区",response = AreaVO.class)
    @ApiImplicitParam(value = "地区ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map child(@PathVariable Long id){
        return ResultHandler.success(areaService.findDirecdChild(id));
    }

    @RequestMapping(value = "/all",  method = RequestMethod.GET)
    @ApiOperation(value = "所有地区",response = AreaVO.class)
    public Map all(){
        return ResultHandler.success(areaService.findChild(null));
    }

    @RequestMapping(value = "/get/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "获取指定地区",response = AreaVO.class)
    @ApiImplicitParam(value = "地区ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map get(@PathVariable Long id){
        return ResultHandler.success(areaService.findById(id));
    }

    @RequestMapping(value = "/save",  method = RequestMethod.POST)
    @ApiOperation(value = "保存地区", notes = "保存字典地区", response = AreaVO.class)
    public Object save(@RequestBody @ApiParam(name="保存地区",value="传入json格式",required=true) AreaVO vo){
        return ResultHandler.success(areaService.save(vo));
    }
    @RequestMapping(value = "/delete/{id}",  method = RequestMethod.PUT)
    @ApiOperation(value = "删除地区", notes = "删除地区", response = Boolean.class)
    @ApiImplicitParam(value = "地区ID" , name = "id" , dataType = "string" , paramType = "path",required=true)
    public Object delete(@PathVariable("id") Long id){
        areaService.delete(id);
        return ResultHandler.success(true);
    }
}
